﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ServerPlatform.Configuration;
using System.Configuration;
using MapEntities;
using System.Xml.Serialization;
using System.IO;
using ServerPlatform.Mapping;
using System.Reflection;

namespace ServerPlatform.Handlers
{
    public class Il
    {
        public int Kod { get; set; }
        public string Ad { get; set; }
        public List<ShapeSegment> Segments { get; set; }
    }

    public class MapDataHandler: IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["t"] == "request")
            {
                string id = context.Request["RequestID"];
                if (!string.IsNullOrEmpty(id))
                {
                    DataProviderSection section = ConfigurationManager.GetSection("DataProviderFramework") as DataProviderSection;
                    IMapDataProvider provider = section.GetMapDataProvider();
                    MapDataRequest request = provider.GetMapData(new Guid(id), context.Request["uri"]);
                    if (request != null)
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(MapDataRequest));
                        context.Response.Clear();
                        context.Response.ContentType = "text/xml";
                        serializer.Serialize(context.Response.OutputStream, request);
                        //context.Response.Flush();
                        context.Response.End();
                    }
                }
            }
            else if (context.Request["t"] == "result")
            {
                string id = context.Request["RequestID"];
                DataProviderSection section = ConfigurationManager.GetSection("DataProviderFramework") as DataProviderSection;
                IMapDataProvider provider = section.GetMapDataProvider();
                MapDataRequest request = provider.GetMapData(new Guid(id), context.Request["uri"]);                
                if (request != null)
                {
                    MapDataResult result = new MapProcessor(request).GetMapData();
                    XmlSerializer serializer = new XmlSerializer(typeof(MapDataResult));
                    context.Response.Clear();
                    context.Response.ContentType = "text/xml";
                    serializer.Serialize(context.Response.OutputStream, result);

                    //CreateCities(result);

                    context.Response.End();
                }
            }
            else if (context.Request["t"] == "client")
            {
                using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ServerPlatform.Resource.VirtualEarth.xap"))
                {
                    byte[] content = new byte[stream.Length];
                    stream.Read(content, 0, content.Length);
                    context.Response.OutputStream.Write(content, 0, content.Length);
                    stream.Close();
                    context.Response.ContentType = "application/x-silverlight-app";                                        
                    context.Response.Flush();
                }
            }
           
        }

        private void CreateCities(MapDataResult result)
        {
            List<Il> iller = new List<Il>(81);
            foreach (var item in result.Shapes)
            {
                Il il = new Il();
                il.Ad = item.Text;

                il.Segments = item.Segments;
                il.Kod = int.Parse(item.Tooltip);
                iller.Add(il);
            }
            StringWriter sw = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(List<Il>));
            serializer.Serialize(sw, iller);
            File.WriteAllText(@"c:\iller.xml", sw.ToString(), Encoding.Unicode);
       
           
        }

        #endregion
    }
}
